
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { ApiService } from 'src/app/core/api';

@Injectable()
export class IndexActivate implements Resolve<any> {
	constructor(private apiService: ApiService) { }
	resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
		// return this.list;
		const code = route.queryParams['code'];
		if(code && code !== localStorage.getItem('code')){
			localStorage.setItem('code', code);
			return this.apiService
			.post('授权码登录', {code})
			.then((e: any) => {
				const token = JSON.parse(e.data);
				localStorage.setItem('gameToken', token['access_token']);
				localStorage.setItem('token', e.data);
				return null;
			})
			.catch(res => {
				return res;
			});
		}else{
			return new Promise((resolve, request) => {
				resolve();
			}).then(() => {
				return null;
			});
		}
	}
}
